﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ReturnToFactoryConfirmFactoryReceive : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            ReturnToFactoryForm form = service.GetReturnToFactoryFormProductDetail(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
            ProviderService providerservice = new ProviderService();
            Provider provider = providerservice.GetProviderByCode(form.ProvideCode);

            if (provider != null)
            {
                providerNameLabel.Text = provider.ProviderName;
                providerCodeLabel.Text = provider.ProviderCode;
                connectPersonLabel.Text = provider.Contact1Name + "," + provider.Contact2Name;
                addressLabel.Text = provider.Address;
                phoneLabel.Text = provider.Contact1Phone + "," + provider.Contact2Phone;
                emailLabel.Text = provider.Contact1Email + "," + provider.Contact2Email;
            }

            runningNumberLabel.Text = form.RunningNumber;
            rtfDateLabel.Text = DateTime.Now.ToString("yyyy-MM-dd");
            rmaCodeLabel.Text = form.ApplicationProduct.ApplicationForm.ApplicationCode;
            productTypeLabel.Text = form.ApplicationProduct.ProductType.ProductTypeName;
            productSerialNumberLabel.Text = form.ApplicationProduct.ProductSerialNumber;
            versionLabel.Text = form.ApplicationProduct.ProductVersionNumber;
            problemDescriptionLabel.Text = form.ApplicationProduct.ProductProblemDescription;
            requireLabel.Text = form.Requirement;
            codeLabel.Text = form.Code;
            tipLabel.Text = form.Tip;
            receiveTextBox.Text = DateTime.Now.ToString("yyyy-MM-dd");
            sendDateLbl.Text = form.SendDate.HasValue ? form.SendDate.Value.ToString("yyyy-MM-dd") : "";
            if (form.IsSecondTimeRepair.HasValue && form.IsSecondTimeRepair.Value)
            {
                isSecondFCLbl.Text = "是";
            }
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(receiveTextBox.Text))
                {
                    ReturnToFactoryService service = new ReturnToFactoryService();
                    ReturnToFactoryForm form = service.GetReturnToFactoryFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
                    if (form.IsSecondTimeRepair.HasValue && form.IsSecondTimeRepair.Value)
                    {
                        form.Status = "返厂-待收货确认";
                    }
                    else
                    {
                        form.Status = "返厂-待维修与报价";
                    }
                    form.FactoryReceiveDate = Convert.ToDateTime(receiveTextBox.Text);
                    form.DifferentInfo = differentInfoTextBox.Text;
                    service.Update(form);

                    ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('保存成功');location.href='ReturnToFactoryListConfirmFactoryReceive.aspx';"), true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('请输入厂商收货日期');", true);
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ReturnToFactoryConfirmFactoryReceive.aspx::saveLinkButton_Click");
                throw ex;
            }
        }
    }
}
